package main.java.cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.proxy.NoOp;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/employee")
public class EmployeeContorler {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    @RequiredPermission(name = "员工页面", expression = "employee:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("departments", departments);
        return "employee/list";
    }

    @RequiredPermission(name = "员工新增/编辑", expression = "employee:input")
    @RequestMapping("/input")
    public String input(Model model, Long id) {
        //共享所有部门的数据
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments", departments);
        //共享所有角色信息
        List<Role> roles = roleService.selectAll();
        model.addAttribute("roles", roles);

        if (id != null) {
            //根据员工id连表查询到员工的信息,在根据根据员工id用额外SQL查询到角色信息(先查到员工中间表再查到角色信息表)
            Employee employee = employeeService.selectByPrimaryKey(id);
            model.addAttribute("employee", employee);
        }

        return "employee/input";
    }

    @RequiredPermission(name = "员工删除", expression = "employee:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            employeeService.deleteByPrimaryKey(id);
        }
        return "redirect:/employee/list";
    }

    @RequiredPermission(name = "员工新增/编辑", expression = "employee:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee, Long[] ids) {

        if (employee.getId() != null) {
            employeeService.updateByPrimaryKey(employee, ids);
        } else {
            employeeService.insert(employee, ids);
        }
        return new JsonResult();
    }

    @RequestMapping("/checkUsername")
    @ResponseBody
    public Map<String, Boolean> checkUsername(String username) {
        HashMap<String, Boolean> map = new HashMap<>();
        //查询数据库是否存在该用户名
        Employee employee = employeeService.selectByUsername(username);
        map.put("valid", employee == null);
        return map;
    }

    @RequiredPermission(name = "员工导出", expression = "employee:exportXls")
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        int status = response.getStatus();
        //指定文件名
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");
        Workbook workbook = employeeService.exportXls();
        workbook.write(response.getOutputStream());
    }

    @RequiredPermission(name = "员工导入", expression = "employee:importXls")
    @RequestMapping("/importXls")
    public String importXls(MultipartFile file) throws Exception {
        employeeService.importXls(file);
        return "redirect:/employee/list";
    }

    //密码重置

    @RequestMapping("/resetPassword")
    public String resetPassword(Long id) {
        employeeService.updatePasswordByEmpId(id);
        return "redirect:/empLogout";
    }

    //跳转到修改密码的页面

    @RequestMapping("/forWordUpdatePage")
    public String forWordUpdatePage() {
        return "common/updatePassword";
    }

    //修改密码

    @RequestMapping("/updatePassword")
    @ResponseBody
    public JsonResult updatePassword(String oldPassword, String newPassword) {
        //登录成功了会存入当前用户session,从session中取出密码
        Employee employee = UserContext.getCurrentUser();
        //如果旧密码和原密码相同则可以修改
        if(employee.getPassword().equals(oldPassword)){
            employeeService.updatePassword(newPassword,employee.getId());

            UserContext.getSession().invalidate();
            return new JsonResult(true,"密码修改成功");
        }
        return  new JsonResult(false,"原密码不正确,密码修改失败");
    }



}
